
from random import randrange
import pmcore.basecom
import sys

class GUIExtPComponent (pmcore.basecom.BaseComponent):
    name = "Sample GUI Extension"
    weight = 10
    requires = "toolbar-control;menu-control;tab-control;text-button;input-field"

    def after_direct_import(self):
        self.smileys = []
        
        tbctrl = get_service("toolbar-control")
        tbctrl.add_btn("Add random smiley", 
                self.settings["ico_folder"]+"add.png",
                self.add_smiley)
        
        mnuctrl = get_service("menu-control")
        mnuctrl.add_menu_item("File->Exit",self.close_app)
        
        txtbtn = get_service("text-button")
        self.btn = txtbtn.new("Add smiley", self.add_smiley)
        self.btn2 = txtbtn.new("Add smiley2", self.add_smiley)
        
        inpfield = get_service("input-field")
        self.if1 = inpfield.new("Input field",onText = self.print_val)
        self.if1.value = 245.36
        
        tabctrl = get_service("tab-control")
        tabctrl.add_tab("Test tab")
        tabctrl.add_element(self.btn,"Test tab")
        tabctrl.add_element(self.btn2,"Test tab")
        tabctrl.add_element(self.if1,"Test tab")
        
    def add_smiley(self,*args, **kwargs):
        smiley = loader.loadModel("smiley")
        smiley.reparentTo(render)
        smiley.setPos(randrange(-20,20),randrange(-20,20),randrange(5,40))
        self.smileys.append(smiley)
    
    def print_val(self,*args, **kwargs):
        print self.if1.value
        
    def close_app(self,*args, **kwargs):
        sys.exit()
